package com.yuexun.beilunpatient.ui.activity.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.ui.activity.adapter.ActivityListAdapter;
import com.yuexun.beilunpatient.ui.activity.ui.Act_Activity_Detail;
import com.yuexun.beilunpatient.utils.DateUtil;

import org.kymjs.kjframe.ui.KJFragment;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2018/4/11 0011.
 */

public class Frag_Activity_Apply extends KJFragment {
    @Bind(R.id.tv_week_title)
    TextView tvWeekTitle;
    @Bind(R.id.tv_week_date)
    TextView tvWeekDate;
    @Bind(R.id.listview)
    ListView listview;

    public Date nowMeetingWeek;
    ActivityListAdapter adapter;

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View view = inflater.inflate(R.layout.frag_activity, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initData() {
        nowMeetingWeek = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE).getTime();
        tvWeekTitle.setText(DateUtil.getWeekOfMonth(nowMeetingWeek));
        String weekDateString = String.format("%s-%s",
                DateFormat.format("MM.dd", DateUtil.getFirstDayOfWeek(nowMeetingWeek)),
                DateFormat.format("MM.dd", DateUtil.getLastDayOfWeek(nowMeetingWeek)));
        tvWeekDate.setText(weekDateString);

        List<String> strings = new ArrayList<>();
        strings.add("");
        strings.add("");
        adapter = new ActivityListAdapter(listview, strings, R.layout.item_activity);
        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                Intent intent = new Intent(getActivity(), Act_Activity_Detail.class);
                bundle.putInt("stat", 2);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.iv_preview, R.id.iv_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_preview:
                previewWeekMeeting();
                break;
            case R.id.iv_next:
                nextWeekMeeting();
                break;
        }
    }

    //上一周
    private void previewWeekMeeting() {
        Calendar calendar = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE);
        calendar.setTime(nowMeetingWeek);
        calendar.add(Calendar.DATE, -7);

        nowMeetingWeek = calendar.getTime();

        tvWeekTitle.setText(DateUtil.getWeekOfMonth(calendar.getTime()));
        String weekDateString = String.format("%s-%s",
                DateFormat.format("MM.dd", DateUtil.getFirstDayOfWeek(nowMeetingWeek)),
                DateFormat.format("MM.dd", DateUtil.getLastDayOfWeek(nowMeetingWeek)));
        tvWeekDate.setText(weekDateString);

        String startDate = DateFormat.format("yyyy-MM-dd", DateUtil.getFirstDayOfWeek(nowMeetingWeek)).toString();
        String endDate = DateFormat.format("yyyy-MM-dd", DateUtil.getLastDayOfWeek(nowMeetingWeek)).toString();

//        mCallBack.searchBack(startDate + "/" + endDate);
    }

    //下一周
    private void nextWeekMeeting() {
        Calendar calendar = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE);
        calendar.setTime(nowMeetingWeek);
        calendar.add(Calendar.DATE, 7);

        nowMeetingWeek = calendar.getTime();

        tvWeekTitle.setText(DateUtil.getWeekOfMonth(calendar.getTime()));
        String weekDateString = String.format("%s-%s",
                DateFormat.format("MM.dd", DateUtil.getFirstDayOfWeek(nowMeetingWeek)),
                DateFormat.format("MM.dd", DateUtil.getLastDayOfWeek(nowMeetingWeek)));
        tvWeekDate.setText(weekDateString);


        String startDate = DateFormat.format("yyyy-MM-dd", DateUtil.getFirstDayOfWeek(nowMeetingWeek)).toString();
        String endDate = DateFormat.format("yyyy-MM-dd", DateUtil.getLastDayOfWeek(nowMeetingWeek)).toString();

//        mCallBack.searchBack(startDate + "/" + endDate);
    }
}
